package com.nondo.dean.community.service;

import com.nondo.dean.common.domain.BaseEntity;
import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.DateProcessUtils;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.community.domain.Article;
import com.nondo.dean.community.domain.ArticleExample;
import com.nondo.dean.community.domain.Category;
import com.nondo.dean.community.mapper.ArticleMapper;
import com.nondo.dean.community.mapper.CategoryMapper;
import com.nondo.dean.community.model.ArticleBean;
import com.nondo.dean.community.model.CategoryBean;

import com.nondo.dean.community.model.result.CategoryArticleResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/24
 */
@Component
public class ArticleService {
    private static String CATEGORY_NAME_SOUP="心灵鸡汤";
    private static String CATEGORY_NAME_ADVERTISING="品牌广告";

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    AliyunOssTool aliyunOssTool;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    CategoryService categoryService;

    /**
     * 添加
     *
     * @param article
     * @return
     */
    public int add(Article article) {
        return this.articleMapper.insertSelective(article);
    }

    /**
     * 更新
     *
     * @param article
     * @return
     */
    @Transactional
    public int update(Article article) {
        return this.articleMapper.updateByPrimaryKeySelective(article);
    }

    /**
     * 删除
     *
     * @param article
     * @return
     */
    public int delete(Article article) {
        article.setDelFlag(Article.DEL_FLAG_DELETE);
        return this.articleMapper.updateByPrimaryKeySelective(article);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public Article select(Integer id) {
        return this.articleMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据名字查询
     *
     * @param title
     * @return
     */
    public Article selectByTitle(String title) {
        ArticleExample brandExample = new ArticleExample();
        brandExample.createCriteria().andTitleEqualTo(title).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);

        List<Article> brandList = this.articleMapper.selectByExample(brandExample);

        return CollectionUtils.isNotEmpty(brandList) ? brandList.get(0) : null;
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param article
     * @return
     */
    public List<Article> getList(int offset, int limit, Article article) {
        ArticleExample example = new ArticleExample();
        example.setOrderByClause("create_date DESC");
        ArticleExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, article);
        RowBounds rowBounds = new RowBounds(offset, limit);
        List<Article> articleList = articleMapper.selectByExampleWithRowbounds(example, rowBounds);

        ArrayList newList = new ArrayList();
        for (Article item : articleList) {
            ArticleBean articleBean = new ArticleBean();
            BeanUtils.copy(item, articleBean);
            //日期处理
            articleBean.setTimeDate(DateProcessUtils.showTimeText(item.getCreateDate()));
            //图片
            articleBean.setImage(this.aliyunOssTool.getImgUrl(articleBean.getImage()));
            //分类
            articleBean.setCategory(this.getCategory(articleBean.getCategoryId()));

            newList.add(articleBean);
        }

        return newList;
    }

    /**
     * 数据统计
     *
     * @param article
     * @return
     */
    public Long count(Article article) {
        ArticleExample example = new ArticleExample();
        ArticleExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, article);
        return articleMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param article
     */
    void setCriteria(ArticleExample.Criteria criteria, Article article) {
        if (article != null) {
                if (article.getId() != null) {
                    criteria.andIdEqualTo(article.getId());
                }
                if (StringUtils.isNoneBlank(article.getTitle())) {
                    criteria.andTitleEqualTo(article.getTitle());
                }

                if (article.getCategoryId() != null && article.getCategoryId() !=0) {
                    criteria.andCategoryIdEqualTo(article.getCategoryId());
                }


        }
        criteria.andDelFlagEqualTo(Article.DEL_FLAG_NORMAL);
    }

    /**
     * 获取分类
     *
     * @param categoryId
     * @return
     */
    public CategoryBean getCategory(int categoryId) {
        Category category = this.categoryMapper.selectByPrimaryKey(categoryId);
        CategoryBean categoryBean = new CategoryBean();
        BeanUtils.copy(category, categoryBean);

        return categoryBean;
    }

    /**
     * 全部分类
     * @return
     */
    public  List<Category> categoryList(){
        List<Category> categoryList =categoryService.getList(new Category());
        Iterator<Category> iterator = categoryList.iterator();
        while (iterator.hasNext()){
            Category beans = iterator.next();
            if(beans == null)continue;
            if (ArticleService.CATEGORY_NAME_SOUP.equals(beans.getName())) {
                iterator.remove();
            }
            if (ArticleService.CATEGORY_NAME_ADVERTISING.equals(beans.getName())) {
                iterator.remove();
            }

        }
        for(Category beans : categoryList){

        }
        Category category = new Category();
        category.setId(0);
        category.setName("全部");
        categoryList.add(0,category);
        return categoryList;
    }

    /**
     * 获取商学院首页数据
     * @param offset
     * @param limit
     * @return
     */
    public List<CategoryArticleResult> commercialCollegeList(int offset, int limit,Integer categoryId){
        List<CategoryArticleResult> newList = new ArrayList<>();
        CategoryArticleResult beans = new CategoryArticleResult();
        newList.add(beans);
        Article article = new Article();
        article.setCategoryId(categoryId);
        if(categoryId !=0){
            beans.setCategoryList(categoryList());
            beans.setArticleList(getList(offset,limit,article));
        }else{
            beans.setCategoryList(categoryList());
            beans.setArticleList(getList(offset,limit,article));
        }

        return newList;
    }
}